<template>
  <div class="layout-container">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="logo">会议室预订系统</div>
      <div class="user-info">
        <el-dropdown>
          <span class="el-dropdown-link">
            <el-avatar :size="30" :src="avatarUrl" class="avatar" />
            {{ userName }}
            <el-icon class="el-icon--right"><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="handleProfile">个人中心</el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <!-- 主内容区 -->
    <div class="main-container">
      <!-- 侧边栏 -->
      <el-aside class="aside" width="200px">
        <el-menu
          :default-active="activeMenu"
          router
          class="menu"
          @select="handleMenuSelect"
        >
          <!-- 客户菜单 -->
          <template v-if="isCustomer">
            <el-menu-item index="/" route="/">
              <el-icon><House /></el-icon>
              <span>首页</span>
            </el-menu-item>
            <el-menu-item index="/rooms" route="/rooms">
              <el-icon><OfficeBuilding /></el-icon>
              <span>会议室列表</span>
            </el-menu-item>
            <el-menu-item index="/orders" route="/orders">
              <el-icon><Document /></el-icon>
              <span>我的订单</span>
            </el-menu-item>
          </template>

          <!-- 管理员菜单 -->
          <template v-if="isAdmin">
            <el-sub-menu index="admin">
              <template #title>
                <el-icon><Setting /></el-icon>
                <span>系统管理</span>
              </template>
              <el-menu-item index="/admin/rooms" route="/admin/rooms">
                <el-icon><OfficeBuilding /></el-icon>
                <span>会议室管理</span>
              </el-menu-item>
              <el-menu-item index="/admin/users" route="/admin/users">
                <el-icon><User /></el-icon>
                <span>用户管理</span>
              </el-menu-item>
            </el-sub-menu>
          </template>

          <!-- 员工菜单 -->
          <template v-if="isEmployee">
            <el-menu-item index="/employee/rooms" route="/employee/rooms">
              <el-icon><OfficeBuilding /></el-icon>
              <span>会议室状态</span>
            </el-menu-item>
            <el-menu-item index="/employee/orders" route="/employee/orders">
              <el-icon><Document /></el-icon>
              <span>订单审核</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>

      <!-- 内容区域 -->
      <div class="content">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import {
  House,
  Document,
  Setting,
  OfficeBuilding,
  User,
  ArrowDown
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

// 计算属性
const isAdmin = computed(() => authStore.isAdmin)
const isEmployee = computed(() => authStore.isEmployee)
const isCustomer = computed(() => authStore.isCustomer)
const userName = computed(() => authStore.name)
const avatarUrl = computed(() => {
  // 这里可以使用实际的用户头像URL
  return 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
})

// 当前激活的菜单项
const activeMenu = computed(() => {
  return route.path
})

// 处理菜单选择
const handleMenuSelect = (index) => {
  console.log('Menu selected:', index)
}

// 处理个人中心
const handleProfile = () => {
  router.push('/profile')
}

// 处理退出登录
const handleLogout = () => {
  authStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.layout-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #409EFF;
  color: white;
  padding: 0 20px;
}

.logo {
  font-size: 18px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  color: white;
  cursor: pointer;
}

.avatar {
  margin-right: 8px;
}

.main-container {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.aside {
  background-color: #fff;
  border-right: 1px solid #e6e6e6;
}

.menu {
  height: 100%;
  border-right: none;
}

.content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #f5f7fa;
}
</style>